package cn.stylefeng.guns.modular.business.frequency.ordercontract.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.ordercontract.entity.OrderContract;
import cn.stylefeng.guns.modular.business.frequency.ordercontract.mapper.OrderContractMapper;
import cn.stylefeng.guns.modular.business.frequency.ordercontract.model.param.OrderContractParam;
import cn.stylefeng.guns.modular.business.frequency.ordercontract.service.OrderContractService;
import cn.stylefeng.guns.modular.business.frequency.ordercontract.enums.OrderContractExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单合同 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:16
 */
@Service
public class OrderContractServiceImpl extends ServiceImpl<OrderContractMapper, OrderContract> implements OrderContractService {

    @Override
    public PageResult<OrderContract> page(OrderContractParam orderContractParam) {

        // 构造条件
        LambdaQueryWrapper<OrderContract> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<OrderContract> list(OrderContractParam orderContractParam) {

        // 构造条件
        LambdaQueryWrapper<OrderContract> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(OrderContractParam orderContractParam) {

        // 将dto转为实体
        OrderContract orderContract = new OrderContract();
        BeanUtil.copyProperties(orderContractParam, orderContract);

        this.save(orderContract);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(OrderContractParam orderContractParam) {
        this.removeById(orderContractParam.getOrderId());
    }

    @Override
    public void edit(OrderContractParam orderContractParam) {

        // 根据id查询实体
        OrderContract orderContract = this.queryOrderContract(orderContractParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(orderContractParam, orderContract);

        this.updateById(orderContract);
    }

    @Override
    public OrderContract detail(OrderContractParam orderContractParam) {
        return this.queryOrderContract(orderContractParam);
    }



    /**
     * 获取订单合同
     *
     * @author yujie
     * @since 2023/05/09 17:16
     */
    private OrderContract queryOrderContract(OrderContractParam orderContractParam) {
        OrderContract orderContract = this.getById(orderContractParam.getOrderId());
        if (ObjectUtil.isEmpty(orderContract)) {
            throw new ServiceException(OrderContractExceptionEnum.NOT_EXISTED);
        }
        return orderContract;
    }
}